package com.eb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eb.annotation.AutoAnnouncement;
import com.eb.annotation.AutoCalculateExp;
import com.eb.context.BaseContext;
import com.eb.domain.Innovation;
import com.eb.domain.Preserve;
import com.eb.dto.PreservePageQueryDTO;
import com.eb.enumeration.OperationType;
import com.eb.exception.BaseException;
import com.eb.mapper.IInnovationMapper;
import com.eb.mapper.IPreserveMapper;
import com.eb.result.PageResult;
import com.eb.service.PreserveService;
import com.eb.vo.PreservePageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PreserveServiceImpl extends ServiceImpl<IPreserveMapper, Preserve> implements PreserveService {

    @Autowired
    private IInnovationMapper iInnovationMapper;

    /**
     * 新建收藏
     * @param innovationId
     */
    @Override
    @AutoAnnouncement(OperationType.PRESERVE)
    @AutoCalculateExp(OperationType.PRESERVE)
    public Boolean addPreserve(Integer innovationId) {
        // 1.判断此贴是否存在
        Innovation innovation = iInnovationMapper.selectById(innovationId);
        if(innovation == null){
            throw new BaseException("帖子不存在");
        }
        // 2.判断此贴是否已经被收藏
        // 构建查询条件
        QueryWrapper<Preserve> wrapper = new QueryWrapper<Preserve>()
                .eq("innovation_id", innovationId)
                .eq("user_id",BaseContext.getCurrentId());
        Preserve p = getOne(wrapper);
        if(p != null){
            deletePreserve(innovationId);
            return false;
        }else {
            // 2.新建收藏
            Preserve preserve = new Preserve();
            preserve.setInnovationId(innovationId);
            preserve.setUserId(BaseContext.getCurrentId());
            preserve.setCreateTime(LocalDateTime.now());
            save(preserve);
        }
        return true;
    }

    /**
     * 分页查询收藏
     * @param preservePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(PreservePageQueryDTO preservePageQueryDTO) {
        // 1.准备分页条件
        int pageNo = preservePageQueryDTO.getPageNo();
        int pageSize = preservePageQueryDTO.getPageSize();
        Page<Preserve> page = Page.of(pageNo, pageSize);
        // 2.查询条件
        page.addOrder(new OrderItem("create_time", false));
        // 2.1根据当前用户id查询收藏
        QueryWrapper<Preserve> queryWrapper = new QueryWrapper<Preserve>()
                .eq("user_id", BaseContext.getCurrentId());
        // 3.分页查询
        Page<Preserve> p = page(page, queryWrapper);
        // 4.解析
        long total = p.getTotal();
        List<Preserve> records = page.getRecords();
        // 5.获取帖子id
        List<Integer> ids = new ArrayList<>();
        for (Preserve record : records) {
            ids.add(record.getInnovationId());
        }
        // 6.封装帖子id与标题
        List<PreservePageQueryVO> preservePageQueryVOS = new ArrayList<>();
        for (Integer id : ids) {
            PreservePageQueryVO preservePageQueryVO = new PreservePageQueryVO();
            Innovation innovation = iInnovationMapper.selectById(id);
            // 判断帖子是否存在
            if(innovation != null){
                BeanUtil.copyProperties(innovation, preservePageQueryVO);
            }else {
                preservePageQueryVO.setTitle("此贴已删除");
            }
            preservePageQueryVOS.add(preservePageQueryVO);
        }
        return new PageResult(total, preservePageQueryVOS);
    }

    /**
     * 取消收藏
     * @param innovationId
     */
    @Override
    public void deletePreserve(Integer innovationId) {
        // 判断帖子是否被收藏
        // 构造查询条件
        QueryWrapper<Preserve> queryWrapper1 = new QueryWrapper<Preserve>()
                .eq("innovation_id", innovationId)
                .eq("user_id", BaseContext.getCurrentId());
        Preserve preserve = getOne(queryWrapper1);
        if(preserve == null){
            throw new BaseException("用户没有收藏此贴！");
        }
        // 构造删除条件
        QueryWrapper<Preserve> queryWrapper2 = new QueryWrapper<Preserve>()
                .eq("innovation_id", innovationId)
                .eq("user_id", BaseContext.getCurrentId());
        remove(queryWrapper2);
    }
}
